package com.qbw.app.module.mine.activity;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;

import com.http.okhttp.HttpTask;
import com.lib.util.ResUtil;
import com.qbw.app.Config;
import com.qbw.app.Constant;
import com.qbw.app.Event;
import com.qbw.app.EventConstant;
import com.qbw.app.R;
import com.qbw.app.databinding.MineActivitySettingBinding;
import com.qbw.app.model.UserInfo;
import com.qbw.app.module.common.activity.CommonActivity;
import com.qbw.app.module.common.activity.DeveloperActivity;
import com.qbw.app.module.common.fragment.LoopItemSelectDialogFragment;
import com.qbw.app.module.common.fragment.OkCancelDialogFragment;
import com.qbw.app.module.user.activity.UserAccountSafeActivity;
import com.qbw.app.module.user.activity.UserLoginRegistActivity;
import com.qbw.app.module.vehicle.activity.VehicleNotificationVehicleListActivity;
import com.qbw.app.request.impl.UserRequest;
import com.qbw.spm.P;

import org.greenrobot.eventbus.EventBus;

import java.util.ArrayList;

import cn.jpush.android.api.JPushInterface;

/*
 *
 * @author QBW
 * @date 2019/8/17
 */
public class MineSettingActivity extends CommonActivity<MineActivitySettingBinding> implements OkCancelDialogFragment.Listener, View.OnClickListener, LoopItemSelectDialogFragment.Listener {

    private UserInfo mUserInfo;

    public static void start(Context context) {
        Intent intent = new Intent(context, MineSettingActivity.class);
        if (!(context instanceof Activity)) {
            intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
        }
        //intent.putExtra(Constant.KEY_USERINFO, userInfo);
        context.startActivity(intent);
    }

    @Override
    protected int getLayoutId() {
        return R.layout.mine_activity_setting;
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        Bundle args = savedInstanceState == null ? getIntent().getExtras() : savedInstanceState;
        /*mUserInfo = (UserInfo) args.getSerializable(Constant.KEY_USERINFO);
        mBinding.setUser(mUserInfo);*/
        mBinding.title.setListener(this);
        boolean isBaidu = Constant.MAP_BAIDU.equals(P.getString(Constant.KEY_MAP,
                                                                Constant.MAP_DEFAULT));
        mBinding.tvMap.setText(isBaidu ? R.string.baidu_map : R.string.google_map);
        boolean isKph = Constant.UNIT_KPH.equals(P.getString(Constant.KEY_UNIT,
                                                             Constant.UNIT_DEFAULT));
        mBinding.tvUnit.setText(isKph ? R.string.unit_kph : R.string.unit_mph);
        mBinding.llAccountSafe.setOnClickListener(this);
        mBinding.llUnit.setOnClickListener(this);
        mBinding.llMap.setOnClickListener(this);
        mBinding.llAbout.setOnClickListener(this);
        mBinding.llOta.setOnClickListener(this);
        mBinding.llFeedback.setOnClickListener(this);
        mBinding.tvLogout.setOnClickListener(this);
        mBinding.llNotification.setOnClickListener(this);
        mBinding.llLanguageSetting.setOnClickListener(this);
        mGeneralManager.getRequestManager().getUserRequest().getUserInfo(this).execute();
    }

    @Override
    public void onClick(View v) {
        int vid = v.getId();
        if (vid == R.id.ll_account_safe) {
            if (mBinding.getUser() != null) {
                UserAccountSafeActivity.startActivity(this, mBinding.getUser());
            }
        } else if (vid == R.id.ll_unit) {
            LoopItemSelectDialogFragment.createInstanceForUnitSelect()
                                        .setListener(this)
                                        .show(getSupportFragmentManager(), "unit_select");
        } else if (vid == R.id.ll_map) {
            LoopItemSelectDialogFragment.createInstanceForMapSelect()
                                        .setListener(this)
                                        .show(getSupportFragmentManager(), "map_select");
        } else if (vid == R.id.ll_about) {
            AboutActivity.start(this);
        }else if (vid == R.id.ll_ota) {
            FirmwareUpgradeActivity.start(this);
        }  else if (vid == R.id.ll_feedback) {

        } else if (vid == R.id.tv_logout) {
            OkCancelDialogFragment.createInstance(0, ResUtil.getString(R.string.logout_tip))
                                  .show(getSupportFragmentManager(), "logout");
        } else if (vid == R.id.ll_notification) {
            VehicleNotificationVehicleListActivity.start(this);
        }else if(vid == R.id.ll_language_setting){
            startActivity(new Intent(this,LanguageActivity.class));
        }
    }

    @Override
    public void onSelectItem(int type, ArrayList<String> items, int selectPos) {
        if (type == LoopItemSelectDialogFragment.TYPE_MAP_SELECT) {
            mBinding.tvMap.setText(items.get(selectPos));
            EventBus.getDefault().post(new Event(EventConstant.MAP_SWITCH));
        } else if (type == LoopItemSelectDialogFragment.TYPE_UNI_SELECT) {
            mBinding.tvUnit.setText(items.get(selectPos));
        }
    }

    @Override
    public void onClickSure(int type) {
        mGeneralManager.getRequestManager()
                       .getCommonRequest()
                       .stopPush(JPushInterface.getRegistrationID(this), null)
                       .execute();
        mGeneralManager.getUserManager().logout();
        UserLoginRegistActivity.start(this);
        finishAffinity();
    }

    @Override
    public void onClickCancel(int type) {

    }

    @Override
    public void onHttpSuccess(HttpTask httpTask, Object entity) {
        super.onHttpSuccess(httpTask, entity);
        String method = httpTask.getMethod();
        if (method.equals(UserRequest.METHOD_USERINFO)) {
            UserInfo.Response response = (UserInfo.Response) entity;
            UserInfo userInfo = response.getData();
            mBinding.setUser(userInfo);
        }
    }

    @Override
    protected void onSaveInstanceState(Bundle outState) {
        super.onSaveInstanceState(outState);
        //outState.putSerializable(Constant.KEY_USERINFO, mUserInfo);
    }
}
